package net.wanho.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import net.wanho.entity.User;
import net.wanho.service.UserService;
import net.wanho.vo.AjaxResult;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * Author：汤小洋
 * Date：2023-12-22 15:32
 * Description：<描述>
 */
@Api(tags = "01.用户模块")
@RestController
@RequestMapping("/users")
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    @ApiOperation("添加用户")
    @PostMapping
    public AjaxResult add(@ApiParam("用户信息") @RequestBody User user) {
        userService.save(user);
        return AjaxResult.success("添加成功", user);
    }

    @ApiOperation("根据主键删除用户")
    @DeleteMapping("/{id}")
    public AjaxResult removeById(@ApiParam("用户id") @PathVariable Long id) {
        userService.removeById(id);
        return AjaxResult.success("删除成功");
    }

    @ApiOperation("查询所有用户")
    @GetMapping
    public AjaxResult findAll() {
        return AjaxResult.success("查询成功", userService.list());
    }

    @ApiOperation("分页查询")
    @GetMapping("/findPage")
    public AjaxResult findPage(@RequestParam(defaultValue = "1") @ApiParam("页码") Integer pageNum,
                               @RequestParam(defaultValue = "10") @ApiParam("页大小") Integer pageSize) {
        Page<User> page = new Page(pageNum, pageSize);
        Page<User> p = userService.page(page);
        return AjaxResult.success("查询成功", p);
    }

    @ApiOperation("修改用户")
    @PutMapping
    public AjaxResult modifyById(@ApiParam("用户信息") @RequestBody User user) {
        userService.updateById(user); // 底层是动态sql，对字段是否为空做了判断
        return AjaxResult.success("修改成功");
    }

    @ApiOperation("根据主键查询用户")
    @GetMapping("/{id}")
    public AjaxResult findById(@ApiParam("用户id") @PathVariable Long id) {
        return AjaxResult.success("查询成功", userService.getById(id));
    }

    @ApiOperation("根据用户名查询")
    @GetMapping("/query/{username}")
    public AjaxResult findByUsername(@ApiParam("要查询的用户名") @PathVariable String username){
        System.out.println("UserController.findByUsername...................");
        return AjaxResult.success();
    }

    @ApiOperation("用户登录")
    @PostMapping("/login")
    public AjaxResult login(@ApiParam("用户名") String username,@ApiParam("密码") String password){
        User user = userService.login(username,password);
        return AjaxResult.success(user);
    }

    // @ApiOperation("多条件查询")
    @ApiIgnore // 忽略该方法
    @GetMapping("/query")
    public AjaxResult findByCondition(@ApiParam("用户名") String username){
        // QueryWrapper<User> wrapper = new QueryWrapper<>();
        // wrapper.like("username",username); // 会自动在左右两侧拼接%
        // wrapper.likeRight("username",username); // 会自动在右侧拼接%
        // wrapper.likeRight(!StringUtils.isEmpty(username), "username", username); // 判断是否为空，不为空才拼接（动态sql）
        // wrapper.ge("id",3);
        // wrapper.or(); // 拼接or
        // wrapper.eq("username",username);
        // wrapper.between("id",1,3);
        // wrapper.in("id",1,2,3);
        // wrapper.orderByDesc("id");

        // 使用Lambda语法
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(User::getUsername, username) // 使用方法引用，可以避免写错字段名
                .gt(User::getId, 3)
                .eq(User::getPassword, "123456");

        return AjaxResult.success(userService.list(wrapper));
    }

    @ApiOperation("多条件分页查询")
    @GetMapping("/page")
    public AjaxResult page(@ApiParam("页码") @RequestParam(defaultValue = "1") Integer pageNum,
                           @ApiParam("页大小") @RequestParam(defaultValue = "10") Integer pageSize,
                           @ApiParam("用户名") String username) {
        Page<User> page = new Page(pageNum, pageSize);

        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(!StringUtils.isEmpty(username), User::getUsername, username);

        userService.page(page, wrapper); // 分页查询，返回值就是传入的page对象，可以不用接收返回值
        return AjaxResult.success("查询成功", page);
    }
}
